package com.hongdee.crm.question.service.Impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.question.dao.ITCrmQuestionGroupDao;
import com.hongdee.crm.question.entity.TCrmSurveryQuestion;
import com.hongdee.crm.question.entity.TCrmSurveryQuestionGroup;
import com.hongdee.crm.question.entity.TCrmSurveryQuestionGroupItem;
import com.hongdee.crm.question.entity.TCrmSurveryQuestionGroupItemArea;
import com.hongdee.crm.question.service.ITCrmQuestionGroupService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Name: 问卷管理 问题分类
 * @Author: Liu XiaoLong
 * @Date: 2017/10/16 10:48
 * @Remark:
 */
@Service
public class TCrmQuestionGroupService implements ITCrmQuestionGroupService {
    @Autowired
    private ITCrmQuestionGroupDao itCrmQuestionGroupDao;
    private String tabName = "T_CRM_SURVERY_QUESTION_GROUP";
    @Override
    public TCrmSurveryQuestionGroup saveOrUpdate(String uid, Map data) throws Exception {
        List list = (List) data.get("dataList");
        String questionId = (String) data.get("questionId");
        String companyId = TokenUtil.getCompanyId(uid);

        // 写入前先删除问卷
        String delItemSql = "DELETE FROM T_CRM_SURVERY_QUESTION_GROUP_ITEM WHERE quesiton_id= '"+questionId+"'";
        itCrmQuestionGroupDao.executeSQL(delItemSql); // 删除问题
        String delAreaSql = "DELETE FROM T_CRM_SURVERY_QUESTION_GROUP_ITEM_AREA WHERE quesiton_id= '"+questionId+"'";
        itCrmQuestionGroupDao.executeSQL(delAreaSql); // 删除选项
        String delGroupSql = "DELETE FROM T_CRM_SURVERY_QUESTION_GROUP WHERE quesiton_id= '"+questionId+"'";
        itCrmQuestionGroupDao.executeSQL(delGroupSql); // 删除问题分类

        for(int i=0 ; i<list.size();i++){
            Map map = (Map) list.get(i);
            /*if(map.get("tType").equals("0")){ // 问题分类项为问题分组
               TCrmSurveryQuestionGroup tCrmSurveryQuestionGroup = new TCrmSurveryQuestionGroup();
               tCrmSurveryQuestionGroup.setGroupId((String) map.get("id"));
               tCrmSurveryQuestionGroup.setQuestionId(questionId);
               tCrmSurveryQuestionGroup.setCompanyId(companyId);
               itCrmQuestionGroupDao.saveOrUpdate(tCrmSurveryQuestionGroup);
            }*/
            if(map.get("tType").equals("1")){ // 问题选项写入的同时将问题分组也写入


                String sql1 = "SELECT * FROM T_CRM_SURVERY_QUESTION_GROUP_ITEM WHERE item_id= '"+ map.get("id")+"' AND quesiton_id= '"+questionId+"'";
                List hasData1 = itCrmQuestionGroupDao.list(sql1);
                if(hasData1.size()<1){
                    // 问题项写入数据库
                    TCrmSurveryQuestionGroupItem tCrmSurveryQuestionGroupItem = new TCrmSurveryQuestionGroupItem();
                    tCrmSurveryQuestionGroupItem.setGroupId((String) map.get("groupId"));
                    tCrmSurveryQuestionGroupItem.setItemId((String) map.get("id"));
                    tCrmSurveryQuestionGroupItem.setCompanyId(companyId);
                    tCrmSurveryQuestionGroupItem.setQuesitonId(questionId);
                    itCrmQuestionGroupDao.saveOrUpdate(tCrmSurveryQuestionGroupItem);
                }

                String areaSql = "SELECT id FROM t_crm_survery_item_area WHERE item_code = '"+map.get("itemCode")+"' ";
                List areaList = itCrmQuestionGroupDao.list(areaSql);

                if(areaList.size()>0){
                    for(int k = 0;k<areaList.size();k++){
                        Map mapTemp = (Map) areaList.get(k);
                        TCrmSurveryQuestionGroupItemArea isqgia = new TCrmSurveryQuestionGroupItemArea();
                        isqgia.setGroupId((String) map.get("groupId"));
                        isqgia.setItemId((String) map.get("id"));
                        isqgia.setAreaId((String) mapTemp.get("id"));
                        isqgia.setCompanyId(companyId);
                        isqgia.setQuesitonId(questionId);
                        itCrmQuestionGroupDao.saveOrUpdate(isqgia);
                    }
                }


                String sql = "SELECT * FROM "+tabName+" WHERE group_id= '"+ map.get("groupId")+"' AND quesiton_id= '"+questionId+"'";
                List hasData = itCrmQuestionGroupDao.list(sql);
                if(hasData.size()<1){
                    // 问题分类写入数据库
                    TCrmSurveryQuestionGroup tCrmSurveryQuestionGroup = new TCrmSurveryQuestionGroup();
                    tCrmSurveryQuestionGroup.setGroupId((String) map.get("groupId"));
                    tCrmSurveryQuestionGroup.setQuestionId(questionId);
                    tCrmSurveryQuestionGroup.setCompanyId(companyId);
                    itCrmQuestionGroupDao.saveOrUpdate(tCrmSurveryQuestionGroup);
                }
            }
        }
        return null;
    }

    /**
     * @Name: 获取左侧数
     * @Author: Liu XiaoLong
     * @Date: 2017/10/16 09:37
     * @Remark:
     */
    @Override
    public Map getLeftTree(String uid, String questionId, Map data) throws Exception {
        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("SELECT a.quesiton_id,a.id,a.group_id,b.disp_order,b.group_code,b.group_name " +
                "FROM t_crm_survery_question_group a LEFT JOIN t_crm_survery_group b ON a.group_id = b.id " +
                "WHERE a.company_id= ? AND quesiton_id = ? ");
        params.add(TokenUtil.getCompanyId(uid));
        params.add(questionId);

        Map map = new HashMap();
        map.put("dataList",itCrmQuestionGroupDao.list(sql.toString(),params.toArray()));

        return map;
    }

    /**
     * @Name: 编辑获取问题
     * @Author: Liu XiaoLong
     * @Date: 2017/10/17 20:48
     * @Remark:
     */
    @Override
    public Map questionData(String uid,String questionId) throws Exception {
        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        StringBuffer sql2 = new StringBuffer();
        List params2 = new ArrayList();

        sql.append("SELECT *,'0' tType FROM t_crm_survery_question_group WHERE quesiton_id = ? AND company_id = ? ");
        params.add(questionId);
        params.add(TokenUtil.getCompanyId(uid));
        List respList = itCrmQuestionGroupDao.list(sql.toString(),params.toArray());

        sql2.append("SELECT *,'1' tType FROM t_crm_survery_question_group_item WHERE quesiton_id = ? AND company_id = ? ");
        params2.add(questionId);
        params2.add(TokenUtil.getCompanyId(uid));
        List tempList  = itCrmQuestionGroupDao.list(sql2.toString(),params2.toArray());
        for(int i=0;i<tempList.size();i++){
            respList.add(tempList.get(i));
        }
        //respList.add();

        Map map = new HashMap();
        map.put("dataList",respList);
        map.put("questionId",questionId);
            return map;
    }
}
